<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Env;
use think\facade\Config;
// 应用公共文件
// 环境检查函数
function check_env(){
    $Envarr=array(
        'os'    =>array('操作系统','无限制','linux',PHP_OS,'success'),
        'phpisv'=>array('PHP版本','5.4','5.5',PHP_VERSION,'success'),
        'gd'    =>array('GD库','2.0','2.0','未知','success'),
        'file'  =>array('附件上传','未限制','2M','未知','success'),
        'pan'   =>array('磁盘空间','100M','>100M','未知','success'),
    );
    // 检查gd库
    $tempArr=function_exists('gd_info')?gd_info():array();
    if(empty($tempArr['GD Version'])){
        $Envarr['gd'][3]='未安装';
        $Envarr['gd'][4]='error'; 
        session('error',true);
    }else{
        $Envarr['gd'][3]=$tempArr['GD Version']; 
    }
    // 检查附件上传
    if(@ini_get('file_uploads')){
        $Envarr['file'][3]=ini_get('upload_max_filesize'); 
    }
    // 磁盘空间
    if(function_exists('disk_free_space')){
        $Envarr['pan'][3]=floor(disk_free_space( Env::get('root_path'))/(1024*1024)).'M'; 
    }
    return $Envarr;
}
// 函数检查
function check_funs(){
    $funs=array(
        // array('mysql_connect','支持','支持','success'),
        array('fsockopen','支持','支持','success'),
        array('gethostbyname','支持','支持','success'),
        array('file_get_contents','支持','支持','success'),
        array('mb_convert_encoding','支持','支持','success'),
        array('json_encode','支持','支持','success'),
    );
    foreach($funs as $k=>$v){
        if(!function_exists($v[0])){
            $funs[$k][2]='不支持';
            $funs[$k][3]='error';
            session('error',true);
        }
    }
    return $funs;
}
// 目录及文件检查
function check_dir(){
    $dirs=array(
        array('dir','runtime','可写','可写','success'),
        array('dir','public/uploads','可写','可写','success'),
        array('file','config/app.php','可写','可写','success'),
        array('file','config/database.php','可写','可写','success'),
    );
    foreach($dirs as $k=>$v){
        if($v[0]=='dir'){
            if(is_dir(Env::get('root_path').$v[1])){
                if(!is_writable(Env::get('root_path').$v[1])){
                    $dirs[$k][3]='可读';
                    $dirs[$k][4]='error';
                    session('error',true);
                }
            }else{
                $dirs[$k][3]='不存在';
                $dirs[$k][4]='error';
                session('error',true);
            }
        }else{
            if(file_exists(Env::get('root_path').$v[1])){
                if(!is_writable(Env::get('root_path').$v[1])){
                    $dirs[$k][3]='可读';
                    $dirs[$k][4]='error';
                    session('error',true);
                }
            }else{
                $dirs[$k][3]='不存在';
                $dirs[$k][4]='error';
                session('error',true);
            }
        }
    }
    return $dirs;
}
//实时输出到界面
function showmsg($msg, $class = '')
{
    echo "<script type=\"text/javascript\">showmsg(\"{$msg}\", \"{$class}\")</script>";
    flush();
    ob_flush();
    sleep(0.5);
}
// 创建表
function create_tables($db,$prefix){
    // 获取数据表文件
    $tables=file_get_contents(Env::get('root_path').'Data/install/install.sql');
    // 获取表前缀
    $oldname=config('database.prefix');
    $sql=str_replace("`{$oldname}","`{$prefix}",$tables);
    $sqlArr=explode(";",$sql);
    foreach($sqlArr as $k=>$v){
        $v=trim($v);
        if(empty($v)) continue;
        if(substr($v,0,12)=='CREATE TABLE'){
            $name=preg_replace("/^CREATE TABLE `(\w+)` .*/s", "\\1", $v);
            $msg="创建数据表{$name}";
            if($db->execute($v)!==false){
                showmsg($msg.'...创建成功','green');
            }else{
                showmsg($msg.'...创建失败','red');
                session('error',true);
            }
        }else{
            $db->execute($v); 
        }
    }
}